function sleep_and_log(duration) {
  var interval = 5;
  while (duration > interval) {
    sleep(interval * 1000);
    console.log("sleep_and_log: interval ", interval, "s");
    duration = duration - interval;
  }
  if (duration > 0) {
    sleep(duration * 1000);
    console.log("sleep_and_log: duration ", duration, "s");
  }
}
function click_text(content) {
  if (exists_text(content)) {
    var uiText = text(content).findOne();
    var bounds = uiText.bounds();
    click(bounds.centerX(), bounds.centerY());
    return uiText;
  } else {
    return false;
  }
}
function click_text_starts_with(content) {
  if (exists_text_starts_with(content)) {
    var uiText = textStartsWith(content).findOne();
    var bounds = uiText.bounds();
    click(bounds.centerX(), bounds.centerY());
    return uiText;
  } else {
    return false;
  }
}
function exists_text(content) {
  if (text(content).exists()) {
    return text(content).findOne();
  } else {
    return false;
  }
}
function exists_text_starts_with(content) {
  if (textStartsWith(content).exists()) {
    return textStartsWith(content).findOne();
  } else {
    return false;
  }
}
function get_center_point(x1, y1, x2, y2) {
  return {
    x: x1 + (x2 - x1) / 2,
    y: y1 + (y2 - y1) / 2,
  };
}
function click_point(x1, y1, x2, y2) {
  var point = get_center_point(x1, y1, x2, y2);
  return click(point.x, point.y);
}
function click_bounds(bounds) {
  click(bounds.centerX(), bounds.centerY());
}
function click_ui(ui_item) {
  while (true) {
    if (ui_item && ui_item.clickable()) {
      ui_item.click();
      return true;
    } else if (ui_item) {
      ui_item = ui_item.parent();
    } else {
      return false;
    }
  }
}
function swipe_up(duration, distance) {
  while (duration > 3) {
    swipe(
      device.width / 2,
      device.height / 2,
      device.width / 2,
      device.height / 2 - distance,
      500 + random(0, 10)
    );
    var interval = 1 + random(0, 2);
    sleep(interval * 1000);
    duration = duration - interval;
  }
  if (duration > 0) {
    sleep(duration * 1000);
  }
  utils.toast("swipe_up end");
}
function utils_toast(content) {
  console.log(content);
  toast(content);
  sleep(6 * 1000);
}
function utils_sleep(duration) {
  var duration = duration / 1000;
  while (duration > 4) {
    sleep(4 * 1000);
    duration = duration - 4;
  }
  if (duration > 0) {
    sleep(duration * 1000);
  }
}
function get_gold(content) {
  var reg_exp = /(\d+)/g;
  var gold_match = content.match(reg_exp);
  var gold = parseInt(gold_match ? gold_match[0] : 0);
  return gold;
}
function get_size_point(points) {
  var x1 = points[0];
  var y1 = points[1];
  var x2 = points[2];
  var y2 = points[3];
  var rate_x = device.width / 720;
  var rate_y = device.height / 1650;
  return [
    parseInt(x1 * rate_x),
    parseInt(y1 * rate_y),
    parseInt(x2 * rate_x),
    parseInt(y2 * rate_y),
  ];
}

var utils = {
  sleep_and_log,
  click_text,
  click_text_starts_with,
  exists_text,
  exists_text_starts_with,
  get_center_point,
  click_point,
  click_bounds,
  click_ui,
  swipe_up,
  toast: utils_toast,
  sleep: utils_sleep,
  get_gold,
  get_size_point,
};

var point_map = {
  首页视频: [147, 1534, 278, 1618],
  首页百度: [16, 1534, 147, 1618],
  首页任务: [278, 1534, 441, 1618],
  进入任务之后的关闭: [582, 518, 606, 542],
  开宝箱之后的关闭: [550, 288, 606, 344],
  开福袋之后的关闭: [632, 344, 686, 400],
  首页福利: [440, 1502, 574, 1618],
};

var READ_START_INDEX = 0;
var READ_EXECUTE_TIMES = 2;
var read = {
  items: [],
  go_page_from_home() {
    this.items = [];
    if (
      !!utils.exists_text("百度") &&
      !!utils.exists_text("听书") &&
      !!utils.exists_text("福利")
    ) {
      console.log("read:go_page_from_home");
      utils.click_text("百度");
      sleep(2 * 1000);
      utils.click_text("热搜");
      sleep(5 * 1000);
      this.start();
    } else {
      console.log("read:go_page_from_home: NOT FOUND");
    }
  },
  start() {
    this.items = id("amx").find();
    if (this.items.length > 0) {
      console.log("read start: 找到【id('amx')】 " + this.items.length);
      this.init();
    } else {
      console.log("read start: 没找到【id('amx')】");
      return;
    }
  },
  init(index) {
    var item = this.items[READ_START_INDEX + index];
    sleep(2 * 1000);
    if (utils.click_ui(item)) {
      console.log("read init: " + index + " 点击成功");
      sleep(2 * 1000);
      this.execute(READ_EXECUTE_TIMES, index);
    } else {
      console.log("read init: " + index + " 点击失败");
      this.next(0);
    }
  },
  execute(duration, index) {
    while (duration > 0) {
      swipe(
        device.width / 2,
        (device.height / 10) * 9,
        device.width / 2,
        (device.height / 10) * 2,
        500 + random(10, 20)
      );
      sleep((1 + random(1, 2)) * 1000);
      duration = duration - 1;
    }
    console.log("read execute: end");
    this.after(index);
  },
  after(index) {
    console.log("read after");
    back();
    this.next(index);
  },
  next(index) {
    if (index == 0) {
      this.init(index + 1);
    }
  },
};
read.go_page_from_home();
